package com.qfedu.service.impl;

import com.qfedu.dao.SysUserMapper;
import com.qfedu.pojo.SysUser;
import com.qfedu.service.SysUserService;
import com.qfedu.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service

public class SysUserServiceImpl implements SysUserService {
     @Autowired
    SysUserMapper sysUserMapper;
    @Override
    public BaseResult login(SysUser sysuser, HttpServletRequest req) {
      SysUser user =sysUserMapper.findByTel(sysuser.getTel());

        BaseResult baseResult = new BaseResult();
        if (user==null) {
            baseResult.setCode(0);
            baseResult.setMessage("用户不存在");
          return baseResult;
        }
        if (!user.getPassword().equals(sysuser.getPassword())){
            baseResult.setCode(0);
            baseResult.setMessage("密码输入错误");
            return baseResult;
        }
        System.out.println(user.getRole());
        if (user.getRole()==1){
            baseResult.setCode(1);
        }else {
            baseResult.setCode(2);
        }

        baseResult.setMessage("登录成功");
        req.getSession().setAttribute("user",user);
        return baseResult;
    }

    @Override
    public BaseResult register(SysUser user,HttpServletRequest req) {
        BaseResult baseResult=new BaseResult();
        SysUser byTel = sysUserMapper.findByTel(user.getTel());
        if (byTel!=null){
            baseResult.setCode(0);
            baseResult.setMessage("用户已存在");
            return baseResult;
        }
        if (!user.getInvitation().equals(req.getSession().getAttribute("captcha"))){


        baseResult.setCode(0);
    baseResult.setMessage("输入验证码有误");
    return baseResult;
    }
    sysUserMapper.add(user);
    baseResult.setCode(1);
    return baseResult;
    }

    @Override
    public BaseResult denglu(HttpServletRequest req) {
         SysUser user = (SysUser) req.getSession().getAttribute("user");
        BaseResult baseResult = new BaseResult();
        baseResult.setInfo(user);
        baseResult.setCode(1);
        return baseResult;
    }
}